import { createRouter, createWebHashHistory, RouteLocationNormalized, RouteRecordRaw } from 'vue-router'
import { jilurizhi } from '../api'
import article from '../ojSass/views/Article/article.vue'
import articleindex from '../ojSass/views/Article/index.vue'
import Assessment from '../ojSass/views/Assessment/index.vue'
import CourseBag from '../ojSass/views/cloudClass/courseBag.vue'
import CourseList from '../ojSass/views/cloudClass/courseList.vue'
import CloudClass from '../ojSass/views/cloudClass/index.vue'
import Material from '../ojSass/views/cloudClass/material.vue'
import WatchCourse from '../ojSass/views/cloudClass/watchCourse.vue'
import Logs from '../ojSass/views/logs/index.vue'
import match from '../ojSass/views/match/index.vue'
import matchend from '../ojSass/views/match/matchend.vue'
import matchindex from '../ojSass/views/match/matchindex.vue'
import matchquestion from '../ojSass/views/match/matchquestion.vue'
import Mynotice from '../ojSass/views/notice/index.vue'
import discuss from '../ojSass/views/Question/discuss.vue'
import questionindex from '../ojSass/views/Question/questionindex.vue'
import questiontype from '../ojSass/views/Question/questiontype.vue'
import Submission from '../ojSass/views/Question/Submission.vue'
import testcases from '../ojSass/views/Question/testcases.vue'
import usersolution from '../ojSass/views/Question/usersolution.vue'
import usertestcase from '../ojSass/views/Question/watchtestcase.vue'
import questionbank from '../ojSass/views/Questionbank/questionbank.vue'
import questionbankindex from '../ojSass/views/Questionbank/questionbankindex.vue'
import TaskList from '../ojSass/views/task/index.vue'
import Task from '../ojSass/views/task/task.vue'
import Mytest from '../ojSass/views/test/index.vue'
import testend from '../ojSass/views/test/testend.vue'
import testindex from '../ojSass/views/test/testindex.vue'
import testquestion from '../ojSass/views/test/testquestion.vue'
import User from '../ojSass/views/User/index.vue'
import integral from '../ojSass/views/User/integral.vue'
import integralreward from '../ojSass/views/User/integralreward.vue'
import Myaddteam from '../ojSass/views/User/myaddteam.vue'
import Mydata from '../ojSass/views/User/mydata.vue'
import Myquestion from '../ojSass/views/User/question.vue'
import Myquestionbank from '../ojSass/views/User/questionbank.vue'
import Mychangequestionbank from '../ojSass/views/User/questionbankindex.vue'
import Mychangequestion from '../ojSass/views/User/questionindex.vue'
import Userqustion from '../ojSass/views/userquestion/index.vue'
import activity from './views/Activity/activity.vue'
import Activitydata from './views/Activity/activitydata.vue'
import activityquestionVue from './views/Activity/activityquestion.vue'
import Activityindex from './views/Activity/index.vue'
import Home from './views/Home.vue'
import index from './views/index.vue'
import Login from './views/login.vue'
import teamfile from './views/team/oss.vue'

function toID(rote: RouteLocationNormalized): Record<string, any> {
  return {
    ID: Number(rote.params.ID)
  }

}

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: '主页',
    component: index,
    // 主页默认进入首页
    redirect: to => {
      return { name: '竞赛' }
    },
    children: [
      {
        path: 'home',
        name: '首页',
        component: Home
      },
      // nav-题库
      {
        path: 'question',
        name: '题库',
        component: questionindex,
        children: []
      },
      {
        path: 'questionDetail/:ID',
        name: '题目',
        component: questiontype,
        props: toID,
        children: [
          {
            path: 'submission',
            name: '我的提交',
            component: Submission
          },
          {
            path: 'discuss',
            name: '题目讨论',
            component: discuss
          },
          {
            path: 'testcases',
            name: '贡献测试点',
            component: testcases
          },
          {
            path: 'usersolution',
            name: '题目题解',
            component: usersolution
          },
        ],
      },
      {
        path: 'changequestion',
        name: '修改我的题库',
        component: Mychangequestion,
      },
      // nav-专题
      {
        path: 'questionbank',
        name: '题单',
        component: questionbankindex
      },
      {
        path: 'changequestionbank/:ID',
        name: '修改我的专题',
        component: Mychangequestionbank,
        props: toID
      },
      // nav-任务
      {
        path: 'task',
        name: '任务',
        component: TaskList,
        props: true,
      },
      {
        path: 'task/:ID',
        name: '任务详情',
        component: Task,
        props: toID
      },
      // nav-比赛
      {
        path: 'activity',
        name: '竞赛',
        component: Activityindex
      },
      // nav-评测
      {
        path: 'assessment',
        name: '评测', // 成员评测记录
        component: Assessment,
        props: true,
      },
      // nav-云课堂
      {
        path: 'cloudClass',
        name: '云课堂',
        component: CloudClass,
        props: true,
        redirect: to => {
          return { name: '我的课堂' }
        },
        children: [
          {
            path: 'courseBag',
            name: '我的课堂',
            components: { default: CourseBag, courseBag: CourseBag },
          },
          {
            path: 'courseList/:ID',
            name: '课程列表',
            // 对于有命名视图的路由，你必须为每个命名视图定义 props 配置
            // props: { default: (route) => ({ ...route.query }), courseList: (route) => ({ ...route.query }) },
            props: { default: toID, courseList: toID },
            components: { default: CourseList, courseList: CourseList },
          },
          {
            path: 'material',
            name: '团队资料',
            components: { default: Material, material: Material },
          },
        ]
      },
      {
        path: 'WatchCourse/:ID',
        name: '观看课程',
        props: (route) => ({ ...route.query }),
        component: WatchCourse,
      },
      // 个人主页
      {
        path: 'user',
        name: '用户',
        component: User,
      },
      {
        path: 'activity/:ID',
        name: '比赛详情',
        component: activity,
        props: toID
      },
      // 测评
      {
        path: 'test/:ID',
        name: '测评',
        component: Mytest,
        props: toID,
        redirect: {
          name: '测评首页'
        },
        children: [
          {
            name: '测评首页',
            path: 'index',
            component: testindex,
          },
          {
            name: '测评答题',
            path: 'question/:index',
            component: testquestion,
            props: true
          },
          {
            name: '测评结束',
            path: 'testend',
            component: testend,
          }
        ]
      },
      // old
      {
        path: 'article/:ID',
        name: '动态详情',
        component: article,
        props: toID
      },
      {
        path: 'article',
        name: '动态',
        component: articleindex
      },
      {
        path: 'activitydata/:ID',
        name: '比赛数据详情',
        component: Activitydata,
        props: toID
      },
      {
        path: 'userqustion',
        name: '答题记录',
        component: Userqustion
      },
      {
        path: 'mydata',
        name: '我的资源',
        component: Mydata,
        redirect: {
          name: '我的题库'
        },
        children: [
          {
            path: 'question',
            name: '我的题库',
            component: Myquestion,
          },
          {
            path: 'questionbank',
            name: '我的题单',
            component: Myquestionbank,
          },
          {
            path: 'myaddteam/:ID',
            name: '加入团队',
            component: Myaddteam,
            props: toID
          },
        ]
      },
      {
        path: 'usertestcase',
        name: '贡献测试点查询',
        component: usertestcase
      },
      {
        path: 'activityquestion/:ID',
        name: '比赛题目',
        component: activityquestionVue,
        props: toID,
      },
      {
        path: 'questionbank/:ID',
        name: '题库列表',
        component: questionbank,
        props: toID
      },

      {
        path: 'integral',
        name: '积分',
        component: integral,
      },
      {
        path: 'integralreward',
        name: '积分兑换',
        component: integralreward,
      },
      {
        path: 'notice',
        name: '我的通知',
        component: Mynotice,
      },
      {
        path: 'match/:ID',
        name: '比赛',
        component: match,
        redirect: {
          name: '比赛首页'
        },
        props: toID,
        children: [
          {
            name: '比赛首页',
            path: 'index',
            component: matchindex,
          },
          {
            name: '比赛答题',
            path: 'question/:index',
            component: matchquestion,
            props: true
          },
          {
            name: '比赛结束',
            path: 'matchend',
            component: matchend,
          }
        ]
      },
      {
        path: 'teamfile',
        name: '团队文件',
        component: teamfile
      }
    ]
  },
  {
    path: '/login',
    name: '登录',
    component: Login
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

router.beforeEach(async (to, from, next) => {
  // 登录者信息
  // const online = sessionStorage.getItem('online')
  // try {
  //   if (to.path !== '/login') {
  //     const onlineRet = await online()
  //   }
  next()
  // } catch (error) {
  //   console.error("beforeEach onlineRet error=>", error)
  //   next('/login')
  // }
})

router.afterEach((to, from) => {
  const arr: [string, string][] = []
  arr[0] = ['type', 'router']
  arr[1] = ['typename1', to.name ? String(to.name) : '']
  arr[2] = ['__from__', from.name ? String(from.name) : '']
  jilurizhi(arr)
})

export default router
